package com.yc.SelectCourse.util;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Collection;
import java.util.Map;


import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

import com.yc.SelectCourse.biz.BizException;


public class StuUtils {
	//存储用户登录信息
	public static Map<String,Object> user=null;
	public static final int num = 48;
	//拉伸图片
	public static void DrawImage(Image image,Canvas canvas) {
		final int imgWidth = image.getBounds().width;
		final int imgHeight = image.getBounds().height;
		canvas.addPaintListener(new PaintListener() {		
			@Override
			public void paintControl(PaintEvent event) {
				event.gc.drawImage(image, 0, 0,imgWidth,imgHeight,0,0,event.width,event.height);	
			}
		});
	}
	//删除composite子组件 以便刷新内容 
	//调用之后调用 requestLayout() 方法
	public static void delectCompositeControl(Composite com) {
		Control[] con = com.getChildren();
		for (Control c : con) {
			c.dispose();
		}
	}
	
	/**
	 * 窗口居中
	 */
	public static void centerWindows(Shell shell) {
		//获取屏幕的大小
		Dimension demo = Toolkit.getDefaultToolkit().getScreenSize();
		shell.setLocation((demo.width-shell.getSize().x)/2, (demo.height-shell.getSize().y)/2);
		
	}
	//
	public static String objToString(Object obj) {
		if(null== obj) {
			return "";
		}
		return obj.toString();
	}
	
	
	public static void check(boolean condition , String msg) throws BizException {
		if(condition) {
			throw new BizException(msg);
		}
	}
	
	public static boolean isEmpty(Object obj) {
		if(obj==null) {
			//判断空对象
			return true;
		}else if( obj instanceof String ) {
			//判断空字符串
			return obj.toString().trim().isEmpty();
		}else if(obj instanceof Collection) {
			//判断集合
			return ((Collection<?>) obj).isEmpty();
		}else if(obj instanceof Map) {
			//判断空字符串
			return ((Map<?,?>) obj).isEmpty();
		}else {
			return false;
		}
	}
	
	
	public static void showMessage(Shell shell,String msg) {
		MessageBox mb =new MessageBox(shell);
		mb.setText("系统提示");
		mb.setMessage(msg);
		mb.open();
	}
	
	//判断分页页数
	public static boolean checkNum(int num , int size) {
		if(num%size==0) {
			return true;
		}else {
			return false;
		}
	}
}
